home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / pthd-0.000 / pthd-0 / pthd-0.7 / src_thread / thread.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-16  |  1.5 KB  |  67 lines

  1. /*
  2.  * --  thread.c.3
  3.  *     Test of async context switching.
  4.  */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <pthread.h>
  8. #include <signal.h>
  9. #include "utils.h"
  10.  
  11. #define ITERATIONS (1000000)
  12. #define THREADS (1)
  13.  
  14. static void 
  15. proc( struct timeval *tv )
  16. {
  17.    long i, sum = 0;
  18.    
  19.    for(i = 0; i < ITERATIONS; i++)
  20.        sum += 1;
  21.  
  22.    i = 0;
  23.    while( --sum >= 0 )
  24.        i += 1;
  25.  
  26.    pthread_exit( (void *) SUCCESS );
  27. }
  28.  
  29. static pthread_t  *th;
  30. static pthread_attr_t attr;
  31.    
  32. int 
  33. main( int argc, char *argv[] )
  34. {
  35.    int thread_count = THREADS, i, exit_status;
  36.    struct sched_param param = { SCHED_ROUND_ROBIN_C,
  37.                                 PRI_RR_DEFAULT,
  38.                                 1                    /* A single quantum */
  39.                               };
  40.  
  41.    if( argc == 2 )
  42.        thread_count = atoi( argv[1] );
  43.  
  44.    (void) pthread_attr_init( &attr );
  45.    (void) pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
  46.    (void) pthread_attr_setschedparam( &attr, ¶m );
  47.  
  48.    (void) pthread_setprio_np( pthread_self(), PRI_RR_MAX );
  49.    th = malloc( thread_count * sizeof( pthread_t * ) );
  50.  
  51.    for(i = 0; i < thread_count; i++ )
  52.        (void) pthread_create( &th[i], &attr, (thread_proc_t) proc, NULL );
  53.  
  54.    for(i = 0; i < thread_count; i++ )
  55.    {
  56.        (void) pthread_join( th[i], (void **) &exit_status );
  57.        if( exit_status != SUCCESS )
  58.            fprintf(stderr, "Failed to join with th[%d]!\n", i);
  59.    }
  60.  
  61.    free( th );
  62.    (void) pthread_attr_destroy( &attr );
  63.  
  64.    print_system_counters();
  65.    return( EXIT_SUCCESS );
  66. }
  67.